import { useQuasar } from '@teld/q-components';
import { cloneDeep } from 'lodash';
export function tableRenderFuctions() {
  let $q = useQuasar();
  //组织数据
  const makeData = (mappingProps, tableRows, tableColumns, inDesign) => {
    if (
      mappingProps.mockData &&
      (!mappingProps.columns || mappingProps.columns.length === 0)
    ) {
      let mockData =
        typeof mappingProps.mockData === 'string'
          ? JSON.parse(mappingProps.mockData)
          : mappingProps.mockData;
      mockData.columns.forEach(element => {
        element.field = element.name;
      });
      tableRows.value = mockData.rows;
      tableColumns.value = mockData.columns;
    } else {
      tableRows.value = Array.isArray(mappingProps.rows)
        ? mappingProps.rows
        : [];
      tableColumns.value = mappingProps.columns
        ? [...mappingProps.columns.filter(item => item.isShow)]
        : [];
      tableColumns.value.forEach(element => {
        if (
          !element.hasOwnProperty('name') &&
          element.hasOwnProperty('dataField')
        ) {
          element.name = element.dataField;
        }
        element.field = element.dataField ?? element.name;
        if (
          !element.hasOwnProperty('label') &&
          element.hasOwnProperty('caption')
        ) {
          //国际化
          if (!window.Funcs) {
            element.label = element.caption;
            return;
          }

          if (element.hasOwnProperty('ValueInfo_caption')) {
            element.label = Funcs.GetTerms(
              element.ValueInfo_caption.ReferenceExpr,
              element.ValueInfo_caption.ShowValue,
            );
          }
        }
        if (!element.hasOwnProperty('align')) {
          element.align = element.alignment ? element.alignment : 'left';
        }
        if (!element.editorType) {
          switch (element.dataType) {
            case 'String':
              element.editorType = 'input';
              break;
            case 'Int':
            case 'Long':
            case 'Decimal':
              element.editorType = 'inputNumber';
              break;
            case 'Boolean':
              element.editorType = 'checkbox';
              break;
            case 'DateTime':
            case 'Date':
            case 'Time':
              element.editorType = 'dateTime';
              break;
          }
        }
      });
      //没有数据时预制数据
      if (inDesign.value && tableColumns.value.length !== 0) {
        let arr = [];
        for (let i = 0; i < 10; i++) {
          let obj = {};
          for (let j of tableColumns.value) {
            if (j.dataType === 'String') {
              obj[j.name] = j.name + i;
            }
          }
          arr.push(obj);
        }
        tableRows.value = arr;;
      }
    }
  };
  //显示序号
  const showRowNumber = (mappingProps, tableColumns) => {
    if (mappingProps.showRowNumber) {
      //要放在输入框后面
      let checkboxIndex = tableColumns.value.findIndex(item => {
        return item.name === '_selected';
      });
      if (checkboxIndex < 0) {
        tableColumns.value.unshift({
          name: 'rowNumber',
          field: 'rowNumber',
          label: $q.lang.editTable?.order,
          editorType: 'rowNumber',
          align: 'left',
          width: '50px',
          // sortable:true,
        });
      } else {
        tableColumns.value.splice(checkboxIndex + 1, 0, {
          name: 'rowNumber',
          field: 'rowNumber',
          label: $q.lang.editTable?.order,
          editorType: 'rowNumber',
          align: 'left',
          width: '50px',
          // sortable: true,
        });
      }
    } else {
      let rowNumberIndex = tableColumns.value.findIndex(item => {
        return item.name === 'rowNumber';
      });
      if (rowNumberIndex >= 0) {
        tableColumns.value.splice(rowNumberIndex, 1);
      }
    }
  };
  //是否允许排序
  const allowSort = (mappingProps, tableColumns) => {
    tableColumns.value.forEach(element => {
      if (
        element.name !== '_selected' &&
        element.name !== '_operation' &&
        element.name !== 'rowNumber' &&
        element.editorType !== 'checkbox'
      ) {
        element.sortable = mappingProps.allowSort;
      }
      // element.sortOrder = 'ad';
    });
  };
  //选择模式
  const checkModel = (mappingProps, tableColumns) => {
    if (
      mappingProps.selection === 'single' ||
      mappingProps.selection === 'multiple'
    ) {
      let checkboxIndex = tableColumns.value.findIndex(item => {
        return item.name === '_selected';
      });
      if (checkboxIndex < 0) {
        tableColumns.value.unshift({
          name: '_selected',
          label: '',
          editorType: 'checkbox',
          align: 'center',
          width: '50px',
        });
      }
    } else {
      let checkboxIndex = tableColumns.value.findIndex(item => {
        return item.name === '_selected';
      });
      if (checkboxIndex > -1) {
        tableColumns.value.splice(checkboxIndex, 1);
      }
    }
  };
  //是否支持换行
  const changeRowsIndex = (mappingProps, tableColumns) => {
    let sortRowIndex = tableColumns.value.findIndex(item => {
      return item.name === 'sortRow';
    });
    //要放在输入框和序号后面
    if (mappingProps.allowColumnWarp) {
      if (sortRowIndex < 0) {
        tableColumns.value.unshift({
          name: 'sortRow',
          label: '',
          editorType: 'btnGroup',
          isShow: true,
          align: 'center',
          width: '50px',
          dataSource: [
            {
              label: '',
              value: 'up',
              icon: TComponents?.IconList?.arrowUp??'icon-t-arrow-up',
              type: 'miniText',
              hint: '上移',
            },
            {
              label: '',
              value: 'down',
              icon: TComponents?.IconList?.arrowDown??'icon-t-arrow-down',
              type: 'miniText',
              hint: '下移',
            },
          ],
        });
      }
    } else {
      if (sortRowIndex >= 0) {
        tableColumns.value.splice(sortRowIndex, 1);
      }
    }
  };
  //是否开启操作列
  const rowOperation = (mappingProps, tableColumns) => {
    if (mappingProps.enableCmdCol) {
      let operationIndex = tableColumns.value.findIndex(item => {
        return item.name === '_operation';
      });
      if (operationIndex < 0) {
        mappingProps.staticItems?.forEach(item => (item.type = 'miniText'));
        tableColumns.value.push({
          name: '_operation',
          label: $q.lang.editTable?.operate,
          editorType: 'btnGroup',
          isShow: true,
          align: 'center',
          width: '',
          dataSource: [
            ...(mappingProps.staticItems ? mappingProps.staticItems : []),
          ],
          btnGroupOptions: {
            moreConf: mappingProps.moreConf,
            autoModel: mappingProps.autoModel,
            moreMaxCount: mappingProps.moreMaxCount,
            btnDropPop: mappingProps.btnDropPop,
            moreBtnIcon:mappingProps.moreBtnIcon,
          },
        });
      }
    } else {
      let operationIndex = tableColumns.value.findIndex(item => {
        return item.name === '_operation';
      });
      if (operationIndex > -1) {
        tableColumns.value.splice(operationIndex, 1);
      }
    }
  };

  return {
    makeData,
    showRowNumber,
    allowSort,
    checkModel,
    changeRowsIndex,
    rowOperation,
  };
}
